package com.itmk.home;

import com.alibaba.fastjson.JSONObject;
import com.itmk.major.entity.MajorResult;
import com.itmk.major.service.MajorService;
import com.itmk.major.service.impl.MajorServiceImpl;
import com.itmk.student.entity.Student;
import com.itmk.student.service.StudentService;
import com.itmk.student.service.impl.StudentServiceImpl;
import com.itmk.teacher.entity.Teacher;
import com.itmk.teacher.service.TeacherService;
import com.itmk.teacher.service.impl.TeacherServiceImpl;
import com.itmk.user.entity.SysUser;
import com.itmk.user.service.SysUserService;
import com.itmk.user.service.impl.SysUserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/home.do")
public class HomeServlet extends HttpServlet {
    private static final String PREX = "WEB-INF/jsp/home";
    private MajorService majorService = new MajorServiceImpl();
    private SysUserService sysUserService = new SysUserServiceImpl();
    private TeacherService teacherService = new TeacherServiceImpl();
    private StudentService studentService = new StudentServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String method = req.getParameter("method");
        if (method.equals("getCount")) {
            getCount(req, resp);
        } else if (method.equals("updatePasswordUI")) {
            updatePasswordUI(req, resp);
        } else if (method.equals("updatePassword")) {
            updatePassword(req, resp);
        }
    }

    private void updatePassword(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        //获取原密码
        String oldPassword = req.getParameter("oldPassword");
        //获取新密码
        String newPassword = req.getParameter("newPassword");
        //获取用户的类型
        HttpSession session = req.getSession();
        int userId = (int) session.getAttribute("userId");
        String userType = (String) session.getAttribute("userType");
        if (userType.equals("1")) {
            Student student = studentService.getUserByPas(userId, oldPassword);
            if (student == null) { //密码错误
                //封装返回值
                JSONObject object = new JSONObject();
                object.put("code", 500);
                object.put("msg", "原密码错误");
                //写回前端
                resp.setContentType("application/json;charset=UTF-8");
                PrintWriter writer = resp.getWriter();
                writer.print(object.toJSONString());
            } else {
                studentService.updatePassword(userId, newPassword);
                 //封装返回值
                JSONObject object = new JSONObject();
                object.put("code", 200);
                object.put("msg", "更能成功");
                //写回前端
                resp.setContentType("application/json;charset=UTF-8");
                PrintWriter writer = resp.getWriter();
                writer.print(object.toJSONString());
            }
        } else if (userType.equals("2")) {
            Teacher student = teacherService.getUserByPas(userId, oldPassword);
            if (student == null) { //密码错误
                //封装返回值
                JSONObject object = new JSONObject();
                object.put("code", 500);
                object.put("msg", "原密码错误");
                //写回前端
                resp.setContentType("application/json;charset=UTF-8");
                PrintWriter writer = resp.getWriter();
                writer.print(object.toJSONString());
            } else {
                teacherService.updatePassword(userId, newPassword);
                 //封装返回值
                JSONObject object = new JSONObject();
                object.put("code", 200);
                object.put("msg", "更能成功");
                //写回前端
                resp.setContentType("application/json;charset=UTF-8");
                PrintWriter writer = resp.getWriter();
                writer.print(object.toJSONString());
            }
        } else if (userType.equals("3")) {
            SysUser student = sysUserService.getUserByPas(userId, oldPassword);
            if (student == null) { //密码错误
                //封装返回值
                JSONObject object = new JSONObject();
                object.put("code", 500);
                object.put("msg", "原密码错误");
                //写回前端
                resp.setContentType("application/json;charset=UTF-8");
                PrintWriter writer = resp.getWriter();
                writer.print(object.toJSONString());
            } else {
                sysUserService.updatePassword(userId, newPassword);
                 //封装返回值
                JSONObject object = new JSONObject();
                object.put("code", 200);
                object.put("msg", "更能成功");
                //写回前端
                resp.setContentType("application/json;charset=UTF-8");
                PrintWriter writer = resp.getWriter();
                writer.print(object.toJSONString());
            }
        }
    }

    private void updatePasswordUI(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.getRequestDispatcher(PREX + "/user_password.jsp").forward(req, resp);
    }

    private void getCount(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        MajorResult majorResult = majorService.majorCount();
        //封装返回值
        JSONObject object = new JSONObject();
        object.put("code", 200);
        object.put("msg", "查询成功");
        object.put("data", majorResult);
        //写回前端
        resp.setContentType("application/json;charset=UTF-8");
        PrintWriter writer = resp.getWriter();
        writer.print(object.toJSONString());
    }
}
